App({
  globalData: {
    openUserInfo: '',
    adminUrl: 'https://bbss.fwxxjs.com/index.php/home/',
    appKey: 'fwk1197etfscfc3714c767e7a19485013116lxnSR',
    isIphoneX: false,
    
    appVision: 2,//版本号
    //用户基础信息
    user_openinfo: '',
    a:'https://bbsss.feiwukeji.com/index.php/home/',
    a: 'http://122.114.127.146/index.php/home/',
  },
  onShow:function(){
    let that = this;
    wx.getSystemInfo({
      success: res => {
        // console.log('手机信息res'+res.model)
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1) {
          that.globalData.isIphoneX = true
        }
      }
    })
 
  },
  onLaunch: function () {
    var that = this;
    that.loginCkeck();
    const updateManager = wx.getUpdateManager();
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调

      console.log(res.hasUpdate);
    });

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate();
          }
        }
      });

    });

    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      wx.showModal({
        title: '更新提示',
        content: '新版本下载失败',
        showCancel: false
      });
    });
  },



  //获取用户基础信息
  getUserInfos() {
    var that = this;
    wx.request({
      url: that.globalData.adminUrl + 'HomeSystem/getUserAppInfo',
      data: {
        trd_session: that.globalData.openUserInfo,
        appkey: that.globalData.appKey

      },
      method: 'GET',
      success: function (res) {
        //console.log(res.data)
        if (res.data.status == 'success') {
          that.globalData.user_openinfo = res.data.user_info;
          
          // 先执行onLaunch  再onload
          if (getCurrentPages().length != 0) {
            getCurrentPages()[getCurrentPages().length - 1].onLoad()
          }

          //console.log(res.data.user_addr)
        }
      },
      fail: function () {
        wx.showToast({
          title: '网络连接失败',
          icon: 'loading',
          duration: 10000
        })

        setTimeout(function () {
          wx.hideToast()
        }, 2000)
      }
    })
  },


  //验证用户是否已经登录
  loginCkeck: function () {
    var that = this;
    //读取本地存储确定是否有用户信息
    try {
      var value = wx.getStorageSync('local_user_session');
      if (value) {
        //获取用户信息后进行相关设置
        that.globalData.openUserInfo = value;
        this.getUserInfos();
      }
      // else{
      //   wx.showModal({
      //     title: '未登录',
      //     content: '请您先登录',
      //     showCancel: false,
      //     success: function (res) {
      //       if (res.confirm) {
      //         wx.switchTab({
      //           url: '/pages/Personal/index/index'
      //         })
      //       }
      //     }
      //   })
      // }

    } catch (e) {
      //读取错误
    }
  },

  //如果后台返回 session_failsession_fail
  //1：清除globalDataglobalData
  //2：清除local_user_session
  stopBehav: function () {
    //清除全局变量 以及本地数据
    var thatt = this
    thatt.globalData.openUserInfo = ''
    wx.removeStorageSync('user_info')
    wx.removeStorageSync('local_user_session')
    //提示用户重新登录
    wx.showModal({
      title: '提示',
      content: '登录失效，请重新登录',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '../personal/index/index',
          })
        }
      }
    })
  },

  //退出登录
  loginOut: function () {
    this.globalData.openUserInfo = '';
    try {
      wx.removeStorageSync('local_user_session')
    } catch (e) {
      // Do something when catch error
    }
    wx.showToast({
      title: '退出成功',
      icon: 'success',
      duration: 1000
    })
    return true;
  },
  //登陆失效失效
  loginExpired: function () {
    this.globalData.openUserInfo = '';
    try {
      wx.removeStorageSync('local_user_session')
    } catch (e) {
      // Do something when catch error
    }
    wx.showModal({
      title: '登录失效',
      content: '请重新登录',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          wx.switchTab({
            url: '/pages/Personal/index/index'
          })
        }
      }
    })
  },

  //提示登陆
  loginFirst: function () {
    if (this.globalData.openUserInfo == '') {
      wx.showModal({
        title: '未登录',
        content: '请您先登录',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/Personal/index/index'
            })
          }
        }
      })
      return false;
    } else {
      return true;
    }

  },

  //判断小程序状态
  getRes: function (res) {
    if (res.state == 'ERROR') {
      wx.navigateTo({
        url: '/pages/Home/error/error?title=小程序出错啦！&content=请联系小程序运营者！'
      })
      return;
    } else if (res.state == 'TIMEOUT') {
      wx.navigateTo({
        url: '/pages/Home/error/error?title=小程序出错啦！&content=请您联系小程序运营者！'
      })
      return;
    } else {
      //不做任何动作
    }
  },

  //判断小程序状态和登录状态
  getResLogin: function (res) {
    if (res.state == 'ERROR') {
      wx.navigateTo({
        url: '/pages/Home/error/error?title=小程序出错啦！&content=请联系小程序运营者！'
      })
      return;
    } else if (res.state == 'TIMEOUT') {
      wx.navigateTo({
        url: '/pages/Home/error/error?title=小程序出错啦！&content=请您联系小程序运营者！'
      })
      return;
    } else if (res.state == 'Login_Expired') {
      this.loginExpired();
      return;
    } else {
      //不做任何动作
    }
  },

  //跳转模块封装
  ikz_navigateBack(e) {
    var types = e.currentTarget.dataset.pathtype;
    var url = e.currentTarget.dataset.pathurl;
    if (types == 'n') {
      wx.navigateTo({
        url: url
      })
    } else if (types == 'd') {
      wx.redirectTo({
        url: url,
      })
    } else if (types == 's') {
      wx.switchTab({
        url: url
      })
    } else if (types == 'ln') {
      if (this.loginFirst()) {
        wx.navigateTo({
          url: url
        })
      }

    } else if (types == 'ld') {
      if (this.loginFirst()) {
        wx.redirectTo({
          url: url,
        })
      }
    }
  }

})